package com.szewec.ps.ui.road.facilities.intersection.task.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.IntDef;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.szewec.ps.R;
import com.szewec.ps.base.BaseApplication;

import org.jetbrains.annotations.NotNull;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class RadioBoxView extends ConstraintLayout {

    protected TextView itemName;
    protected View llTrue;
    protected View llFalse;
    protected TextView btnTrue;
    protected TextView btnFalse;
    protected View underLine;

    private int checked = ONE;

    public RadioBoxView(Context context) {
        super(context);
        init(context, null);
    }

    public RadioBoxView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RadioBoxView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.view_radio_box, this);
        itemName = findViewById(R.id.tv_item_name);
        llTrue = findViewById(R.id.ll_true);
        llFalse = findViewById(R.id.ll_false);
        btnTrue = findViewById(R.id.tv_true);
        btnFalse = findViewById(R.id.tv_false);
        underLine = findViewById(R.id.v_underline);
        parseStyle(context, attrs);
        setOnclick();
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RadioBoxView);
            String h = ta.getString(R.styleable.RadioBoxView_itemName);
            itemName.setText(h);

            boolean required = ta.getBoolean(R.styleable.RadioBoxView_required, false);

            if (!required) {
                itemName.setCompoundDrawables(null, null, null, null);
            }

            checked = ta.getInt(R.styleable.RadioBoxView_checked, 1);
            if (checked == 0) {
                setChecked(ONE);
            } else {
                setChecked(TWO);
            }

            boolean showUnderline = ta.getBoolean(R.styleable.RadioBoxView_underline, false);
            if (showUnderline) {
                underLine.setVisibility(VISIBLE);
            } else {
                underLine.setVisibility(GONE);
            }

            String oneName = ta.getString(R.styleable.RadioBoxView_oneName);
            btnTrue.setText(oneName);
            String twoName = ta.getString(R.styleable.RadioBoxView_twoName);
            btnFalse.setText(twoName);

            ta.recycle();
        }
    }

    private void setOnclick() {
        llTrue.setOnClickListener(v -> setChecked(ONE));
        llFalse.setOnClickListener(v -> setChecked(TWO));
    }

    /**
     * 设置是否必填
     *
     * @param required
     */
    public void setRequired(boolean required) {
        if (required) {
            Drawable drawable = ContextCompat.getDrawable(BaseApplication.getContext(), R.drawable.red_star);
            itemName.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
        } else {
            itemName.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }
    }

    public void setOneText(CharSequence text) {
        btnTrue.setText(text);
    }

    /**
     * 设置enable
     *
     * @param enabled
     */
    public void setEnabled(boolean enabled) {
        if (!enabled) {
            llTrue.setOnClickListener(null);
            llFalse.setOnClickListener(null);
        } else {
            setOnclick();
        }
    }

    private CheckedChangeListener listener;

    public interface CheckedChangeListener {
        void change(@checked Integer checkedPosition);
    }

    public void setCheckedChangeListener(CheckedChangeListener listener) {
        this.listener = listener;
    }

    public @checked
    int getChecked() {
        return checked;
    }

    public final static int ONE = 0;

    public final static int TWO = 1;

    /**
     * @hide
     */
    @IntDef({ONE, TWO})
    @Retention(RetentionPolicy.SOURCE)
    public @interface checked {
    }

    public void setChecked(@checked @NotNull Integer checked) {
        this.checked = checked;
        if (checked == 0) {
            btnTrue.setBackgroundResource(R.drawable.bg_checked_box);
            btnFalse.setBackgroundResource(R.drawable.bg_unchecked_box);
        } else {
            btnTrue.setBackgroundResource(R.drawable.bg_unchecked_box);
            btnFalse.setBackgroundResource(R.drawable.bg_checked_box);
        }
        if (listener != null) {
            listener.change(this.checked);
        }
    }
}
